package com.piedra.xml;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XMLParser {
	
	public static void main(String[] args) throws DocumentException, IOException {
		
		SAXReader reader = new SAXReader();
		Document doc = reader.read(new File("D:/workspace/Piedra/src/com/piedra/xml/web.xml"));
		Element rootElement = doc.getRootElement();
		System.out.println(rootElement.getName());
		
		createXMLFile();
	}

	private static void createXMLFile() throws IOException {
		Document doc = DocumentHelper.createDocument();
		Element root = doc.addElement("root");
		root.addElement("root_1").addElement("root_1_1").addAttribute("a", "aaa");
		root.addElement("root_2");
		root.addAttribute("name", "rootName").addAttribute("pwd", "password");
		
		FileWriter writer = new FileWriter("test.xml");
		writer.flush();
		doc.write(writer);
	
		writer.close();
	}
}
